﻿// -----------------------------------------------------------------------
// <copyright file="Guard.cs" company="Henadzi Sabaleuski.">
// Copyright (c) Henadzi Sabaleuski. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace HSabaleuski.Utilities.Diagnostics
{
	using System;
	using System.Diagnostics;
	using System.Globalization;

	/// <summary>
	/// The class represent of Guard
	/// </summary>
	public static class Guard
	{
		/// <summary>
		/// Verifies that variables is not null.
		/// </summary>
		/// <typeparam name="T">Any class of variable</typeparam>
		/// <param name="parameter">The variable.</param>
		/// <exception cref="ArgumentNullException">Throw Argument Null Exception if parameter is null</exception>
		public static void VerifyNullable<T>(T parameter) where T : class
		{
			if (parameter == null)
			{
				throw new ArgumentNullException(string.Format(CultureInfo.InvariantCulture, "Invalid parameter was called from {0} method", new StackFrame(1).GetMethod().Name));
			}
		}

		/// <summary>
		/// Verifies that variable satisfies the condition
		/// </summary>
		/// <typeparam name="T">Any class of variable</typeparam>
		/// <param name="parameter">The parameter.</param>
		/// <param name="validationCondition">The validation condition.</param>
		/// <exception cref="ArgumentNullException">Throw Argument Null Exception if validationCondition is null</exception>
		public static void VerifyCondition<T>(T parameter, Predicate<T> validationCondition) where T : class
		{
			if (validationCondition == null)
			{
				throw new ArgumentNullException("validationCondition");
			}

			if (!validationCondition.Invoke(parameter))
			{
				throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid parameter was called from {0} method", new StackFrame(1).GetMethod().Name));
			}
		}
	}
}
